package org.example;

import java.util.Arrays;

/**
 * 128最长连续序列
 */
public class LongestConsecutive {
    public int longestConsecutive(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        int[] sorted = Arrays.stream(nums).sorted().toArray();
        int max = 1;
        int count = 1;
        for (int i = 0; i < sorted.length - 1; i++) {
            if (sorted[i] + 1 == sorted[i + 1]) {
                count++;
            } else if (sorted[i] < sorted[i + 1]) {
                count = 1;
            }
            if (count > max) {
                max = count;
            }
        }
        return max;
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 0, 1};
        LongestConsecutive longestConsecutive = new LongestConsecutive();
        System.out.println(longestConsecutive.longestConsecutive(nums));
    }
}
